***********************************************
******************* ANALYSIS ******************
***********************************************
/*
In this do file, we create all the tables, graphs
and results of the paper
*/

clear all
set more off

ssc install coefplot, replace
ssc install outreg2, replace
set scheme cleanplots

*----------------
* PATHS
*----------------
global replication `c(pwd)'
global data "$replication\Data"
global inter "$replication\Dta - Intermediate"
global final "$replication\Dta - Final"
global graphs "$replication\Graphs"
global tables "$replication\Tables"

* cd "$final"
use "$inter\PricesAuctionsMerged_New.dta"

* ===========================================
* FIGURE 1 - Issuances by Maturity (Panels)
* ===========================================
net install cleanplots, from("https://tdmize.github.io/data/cleanplots")
set scheme cleanplots, perm
graph set window fontface "Garamond"

* PANEL A 
preserve 
	keep if mon2mat<=20
	scatter mon2mat iss_my,  msize(small) graphregion(c(white )ic(white) ifc(white) ilc(white)) ylabel(#10, labsize(big)) xlabel(#6, labsize(big)) mcolor(black)  saving("$graphs\Paper_graphs\F1_Panel_A.gph", replace) 
restore

preserve 
	keep if mon2mat>=20
	* PANEL B
	scatter y2mat iss_my if y2mat<5 & y2mat>3,  msize(small) graphregion(c(white )ic(white) ifc(white) ilc(white)) ylabel(#3, labsize(big)) xlabel(#6, labsize(big)) mcolor(black) saving("$graphs\Paper_graphs\F1_Panel_B.gph", replace) 
	* PANEL C
	scatter y2mat iss_my if mon2mat<=200,  msize(small) graphregion(c(white )ic(white) ifc(white) ilc(white)) ylabel(#10, labsize(big)) xlabel(#6, labsize(big)) mcolor(black) saving("$graphs\Paper_graphs\F1_Panel_C.gph", replace) 
	* PANEL D 
	scatter y2mat iss_my if mon2mat>200,  msize(small) graphregion(c(white )ic(white) ifc(white) ilc(white)) ylabel(#10, labsize(big)) xlabel(#6, labsize(big)) mcolor(black) saving("$graphs\Paper_graphs\F1_Panel_D.gph", replace) 
restore

* Combining the 4 panels
gr combine "$graphs\Paper_graphs\F1_Panel_A" "$graphs\Paper_graphs\F1_Panel_B" "$graphs\Paper_graphs\F1_Panel_C" "$graphs\Paper_graphs\F1_Panel_D", graphregion(c(white )ic(white) ifc(white) ilc(white)) iscale(0.45) 
graph save Graph "$graphs\Paper_graphs\Figure1.gph", replace
graph export "$graphs\Paper_graphs\Figure1.pdf", as(pdf) replace

*====================================================
* FIGURE 2 - Months to maturity & estimates of lambda
*====================================================
use "$inter\PricesAuctionsMerged_New.dta", clear

* Issuance Quarter
gen iss_qt=quarter(date)
label var iss_qt "Issuance Quarter"
gen qt_date=qofd(dates)
label var qt_date "Quarter Date"

* Secondary Market Price
gen mid_price=(oprice+cprice)/2

* Average Price in Auction
gen averprice=(mar_totalot*marginprice+wavprice*(1-mar_totalot))

gen price_gap=(1-(wavprice/mid_price))*100*100
gen markup_wav=(1-(wavprice/mid_price))*100*100
gen markup_ave=(1-(averprice/mid_price))*100*100
gen markup_mar=(1-(marginprice/mid_price))*100*100
label var price_gap "Markup (bps)"

replace totalot_ngdp=totalot_ngdp*100
label var totalot_ngdp "Issuance (% Monthly GDP)"

drop if totalot <1000000

set matsize 4000 
set more off 
set scheme cleanplots
graph set window fontface "Garamond"

gen matgroup=rmon2mat 
label var matgroup "Maturity Category"

replace matgroup=3   if(matgroup>=0&matgroup<=4)
replace matgroup=6   if(matgroup>=5&matgroup<=7)
replace matgroup=9   if(matgroup>=8&matgroup<=10)
replace matgroup=12  if(matgroup>=11&matgroup<=14)
replace matgroup=18  if(matgroup>=15&matgroup<=20)
replace matgroup=36  if(matgroup>=21&matgroup<=44)
replace matgroup=60  if(matgroup>=45&matgroup<=74)
replace matgroup=120 if(matgroup>=75&matgroup<=144)
replace matgroup=180 if(matgroup>=145&matgroup<=210)
replace matgroup=360 if(matgroup>211&matgroup<=410)
replace matgroup=600 if(matgroup>410)

gen Bill_G=1
replace Bill_G=0 if(matgroup>18)
gen Year_G=0
replace Year_G=1 if(matgroup<=12)

gen G3Dummy=0
gen G6Dummy=0
gen G9Dummy=0
gen G12Dummy=0
gen G18Dummy=0
gen G36Dummy=0
gen G60Dummy=0
gen G120Dummy=0
gen G180Dummy=0
gen G360Dummy=0
gen G600Dummy=0

* By group dummy
replace G3Dummy=1   if(matgroup==3)
replace G6Dummy=1   if(matgroup==6)
replace G9Dummy=1   if(matgroup==9)
replace G12Dummy=1  if(matgroup==12)
replace G18Dummy=1  if(matgroup==18)
replace G36Dummy=1  if(matgroup==36)
replace G60Dummy=1  if(matgroup==60)
replace G120Dummy=1 if(matgroup==120)
replace G180Dummy=1 if(matgroup==180)
replace G360Dummy=1 if(matgroup==360)
replace G600Dummy=1 if(matgroup==600)

gen IssDummy3=G3Dummy*totalot_ngdp 
gen IssDummy6=G6Dummy*totalot_ngdp 
gen IssDummy9=G9Dummy*totalot_ngdp 
gen IssDummy12=G12Dummy*totalot_ngdp 
gen IssDummy18=G18Dummy*totalot_ngdp 
gen IssDummy36=G36Dummy*totalot_ngdp 
gen IssDummy60=G60Dummy*totalot_ngdp 
gen IssDummy120=G120Dummy*totalot_ngdp 
gen IssDummy180=G180Dummy*totalot_ngdp 
gen IssDummy360=G360Dummy*totalot_ngdp 
gen IssDummy600=G600Dummy*totalot_ngdp

gen Bill_IssDummy=Bill_G*totalot_ngdp 
gen Year_IssDummy=Year_G*totalot_ngdp 
gen MYear_IssDummy=(1-Year_G)*totalot_ngdp 
label var totalot_ngdp "Issuance (% Monthly GDP) "

label var IssDummy3   "3" 
label var IssDummy6   "6" 
label var IssDummy9   "9" 
label var IssDummy12  "12" 
label var IssDummy18  "18" 
label var IssDummy36  "36" 
label var IssDummy60  "60" 
label var IssDummy120 "120" 
label var IssDummy180 "180" 
label var IssDummy360 "360" 
label var IssDummy600 "600" 
label var Bill_IssDummy "Bill" 


label var G3Dummy   "3" 
label var G6Dummy   "6" 
label var G9Dummy   "9" 
label var G12Dummy  "12" 
label var G18Dummy  "18" 
label var G36Dummy  "36" 
label var G60Dummy  "60" 
label var G120Dummy "120" 
label var G180Dummy "180" 
label var G360Dummy "360" 
label var G600Dummy "600" 
label var Bill_G "Bill"

gen G3Dummy_out=0
gen G6Dummy_out=0
gen G9Dummy_out=0
gen G12Dummy_out=0
gen G18Dummy_out=0
gen G36Dummy_out=0
gen G60Dummy_out=0
gen G120Dummy_out=0
gen G180Dummy_out=0
gen G360Dummy_out=0
gen G600Dummy_out=0

* By group dummy
replace G3Dummy_out=1   if(matgroup==3)
replace G6Dummy_out=1   if(matgroup==6)
replace G9Dummy_out=1   if(matgroup==9)
replace G12Dummy_out=1  if(matgroup==12)
replace G18Dummy_out=1  if(matgroup==18)
replace G36Dummy_out=1  if(matgroup==36)
replace G60Dummy_out=1  if(matgroup==60)
replace G120Dummy_out=1 if(matgroup==120)
replace G180Dummy_out=1 if(matgroup==180)
replace G360Dummy_out=1 if(matgroup==360)
replace G600Dummy_out=1 if(matgroup==600)

gen OutDummy3=G3Dummy_out*out_ngdp 
gen OutDummy6=G6Dummy_out*out_ngdp 
gen OutDummy9=G9Dummy_out*out_ngdp 
gen OutDummy12=G12Dummy_out*out_ngdp 
gen OutDummy18=G18Dummy_out*out_ngdp 
gen OutDummy36=G36Dummy_out*out_ngdp 
gen OutDummy60=G60Dummy_out*out_ngdp 
gen OutDummy120=G120Dummy_out*out_ngdp 
gen OutDummy180=G180Dummy_out*out_ngdp 
gen OutDummy360=G360Dummy_out*out_ngdp 
gen OutDummy600=G600Dummy_out*out_ngdp

bys isin iss_my matgroup: egen OutGDP = sum(out_ngdp)

*gen Bill_OutDummy=Bill_G_out*out_ngdp 
*gen Year_OutDummy=Year_G_out*out_ngdp 
label var out_ngdp "Outstanding (% Monthly GDP) "

label var OutDummy3   "3" 
label var OutDummy6   "6" 
label var OutDummy9   "9" 
label var OutDummy12  "12" 
label var OutDummy18  "18" 
label var OutDummy36  "36" 
label var OutDummy60  "60" 
label var OutDummy120 "120" 
label var OutDummy180 "180" 
label var OutDummy360 "360" 
label var OutDummy600 "600" 
* label var Bill_OutDummy "Bill" 

label var G3Dummy_out   "3" 
label var G6Dummy_out   "6" 
label var G9Dummy_out   "9" 
label var G12Dummy_out  "12" 
label var G18Dummy_out  "18" 
label var G36Dummy_out  "36" 
label var G60Dummy_out  "60" 
label var G120Dummy_out "120" 
label var G180Dummy_out "180" 
label var G360Dummy_out "360" 
label var G600Dummy_out "600" 
*label var Bill_G_out "Bill"

* Drop 50 years
drop if(G600Dummy==1)
drop IssDummy600

areg price_gap i.matgroup IssDummy*, absorb(qt_date) vce(r)

coefplot, vertical keep(IssDummy*) msymbol(d) mcolor(red) levels(99 95 90) ciopts(lwidth(3 ..) color(black black black) lcolor(*.2 *.4 *.6 *.8 *1)) xtitle(" Months to Maturity", size(huge)) ytitle("Estimates  of   {&Lambda} ({&tau})", size(huge)) xline(5.5)
graph save Graph "$graphs\Paper_graphs\Figure2.gph", replace
graph export "$graphs\Paper_graphs\Figure2.pdf", as(pdf) replace

reg price_gap IssDummy*, absorb(qt_date) vce(r)

coefplot, vertical keep(IssDummy*) msymbol(d) mcolor(red) levels(99 95 90) ciopts(lwidth(3 ..) color(black black black) lcolor(*.2 *.4 *.6 *.8 *1)) xtitle(" Months to Maturity", size(huge)) ytitle("Estimates  of   {&Lambda} ({&tau})", size(huge)) xline(5.5)
graph save Graph "$graphs\Paper_graphs\Figure2_alt.gph", replace
graph export "$graphs\Paper_graphs\Figure2_alt.pdf", as(pdf) replace


* regression table
cd "$tables"
areg price_gap i.matgroup IssDummy*, absorb(qt_date) vce(r)
outreg2 using reg_nonparam, replace tex dec(2)  label title("By Group")
coefplot, vertical keep(IssDummy*) msymbol(d) mcolor(red) levels(99 95 90) ciopts(lwidth(3 ..) lcolor(*.2 *.4 *.6 *.8 *1)) xtitle(" Months to Maturity", size(huge)) ytitle(" Markup (bps) over % Issuance of GDP", size(huge)) xline(5.5)
graph export "$graphs\Paper_graphs\RegCoeffsByMat.pdf", as(pdf) replace
areg price_gap i.matgroup IssDummy* if(iss_yr<=2010), absorb(qt_date) vce(r)
outreg2 using reg_nonparam, append tex dec(2)  label title("By Group")
areg price_gap i.matgroup IssDummy* if(iss_yr>2012), absorb(qt_date) vce(r)
outreg2 using reg_nonparam, append tex dec(2)  label title("By Group")

* no intercept version
areg price_gap IssDummy*, absorb(qt_date) vce(r)
outreg2 using reg_nonparam_nofe, replace tex dec(2)  label title("By Group")
coefplot, vertical keep(IssDummy*) msymbol(d) mcolor(red) levels(99 95 90) ciopts(lwidth(3 ..) lcolor(*.2 *.4 *.6 *.8 *1)) xtitle(" Months to Maturity", size(huge)) ytitle(" Markup (bps) over % Issuance of GDP", size(huge)) xline(5.5)
graph export "$graphs\Paper_graphs\RegCoeffsByMat_noFE.pdf", as(pdf) replace
areg price_gap IssDummy* if(iss_yr<=2010), absorb(qt_date) vce(r)
outreg2 using reg_nonparam_nofe, append tex dec(2)  label title("By Group")
areg price_gap IssDummy* if(iss_yr>2012), absorb(qt_date) vce(r)
outreg2 using reg_nonparam_nofe, append tex dec(2)  label title("By Group")

* Regression 1: Linear Regression with Maturity Trend  (excluding Bills)
areg price_gap i.matgroup totalot_ngdp c.totalot_ngdp#c.y2mat, absorb(qt_date) vce(r)
outreg2 using reg_linearcoeffs_year, replace tex dec(2) $efectos_si $drop  label title("By Group")
areg price_gap i.matgroup totalot_ngdp Year_IssDummy c.totalot_ngdp#c.y2mat if(iss_yr<=2010),  absorb(qt_date) vce(r)
outreg2 using reg_linearcoeffs_year, tex dec(2) $efectos_si $drop  label title("By Group")
areg price_gap i.matgroup totalot_ngdp Year_IssDummy c.totalot_ngdp#c.y2mat if(iss_yr>=2012),  absorb(qt_date) vce(r)
outreg2 using reg_linearcoeffs_year, tex dec(2) $efectos_si $drop  label title("By Group")

cd "$replication"
*======================================
* FIGURE 4 - Issuance Pattern (Panels)
*======================================
use "$final\Master_QuarterlyData.dta", clear

graph set window fontface "Garamond"

* Setting data as quarterly series
tsset qy
format qy %tq
gen year = yofd(dofq(date)) 
label var year "Year"
label var avmon_mat "Average Maturity of Issuance"

* Generating ratios of GDP
gen Issue_GDP=issuance/GDP/1000000*100
gen Prin_GDP=principal/GDP/1000000*100
gen Deficit_GDP=-Surplus_GDP*100

label var Issue_GDP "Issuances / GDP (quart, %)"
label var Prin_GDP "Debt Due / GDP (quart, %)"
label var Deficit_GDP "Deficit / GDP (quart, %)"

replace rbeta=rbeta
replace sbeta=sbeta
label var rbeta "Short-term Rate Factor (%)"
label var sbeta "Slope Factor (%)"

/* PANEL A - Quarterly debt issuances, principal amortizations, and primary 
deficit as fractions of GDP (2001 q1 - 2017 q3) */
twoway (tsline Issue_GDP Deficit_GDP Prin_GDP if(year>2000&year<2018), yaxis(1) ylabel(, labsize(big)) xlabel(, labsize(big)) lcolor(black) saving("$graphs\Paper_graphs\F4_Panel_A.gph", replace)), leg(pos(5) rows(1) size(*1.25))
graph export "$graphs\Paper_graphs\F4_Panel_A.pdf", as(pdf) replace

/* PANEL B - Weighted yearly series for the average maturity in the year 
against level and slope factors of the yield curve */
preserve
	collapse (mean) av* rbeta sbeta sp_s* sp_r*, by(year)
	tsset year

	twoway (tsline av* if(year>2001&year<2017), yaxis(1) ylabel(, labsize(big)) lcolor(black) xlabel(, labsize(big))) (tsline rbeta sbeta if(year>2001&year<2017), yaxis(2) saving("$graphs\Paper_graphs\F4_Panel_B.gph", replace)), leg(pos(5) rows(1) size(*1.25))
	graph export "$graphs\Paper_graphs\F4_Panel_B.pdf", as(pdf) replace

restore

/* PANEL C - Regression coefficients of q.issuances and maturity against the 
quarterly principal amortizations, primary deficit, and the average of daily 
level and slope factors */
reg Issue_GDP Deficit_GDP Prin_GDP rbeta sbeta
coefplot , xlabel(, labsize(small)) vertical drop(_cons) msymbol(d) mcolor(red) levels(99 95 90) ciopts(color(black black black) lwidth(3 ..)  lcolor(*.2 *.4 *.6 *.8 *1) ) xtitle("Variable") ytitle("Coefficient") saving("$graphs\Paper_graphs\F4_Panel_C.gph", replace)
graph export "$graphs\Paper_graphs\F4_Panel_C.pdf", as(pdf) replace

/* PANEL D - Regression coefficients of q.issuances and maturity against the 
quarterly principal amortizations, primary deficit, and the average of daily 
level and slope factors */
reg av* Deficit_GDP Prin_GDP rbeta sbeta
coefplot , xlabel(, labsize(small)) vertical drop(_cons) msymbol(d) mcolor(red) levels(99 95 90) ciopts(color(black black black) lwidth(3 ..) lcolor(*.2 *.4 *.6 *.8 *1)) xtitle("Variable") ytitle("Coefficient") saving("$graphs\Paper_graphs\F4_Panel_D.gph", replace)
graph export "$graphs\Paper_graphs\F4_Panel_D.pdf", as(pdf) replace

* Combining the 4 panels
gr combine "$graphs\Paper_graphs\F4_Panel_A" "$graphs\Paper_graphs\F4_Panel_B" "$graphs\Paper_graphs\F4_Panel_C" "$graphs\Paper_graphs\F4_Panel_D", graphregion(c(white )ic(white) ifc(white) ilc(white)) iscale(0.45) 
graph save Graph "$graphs\Paper_graphs\Figure4.gph", replace
graph export "$graphs\Paper_graphs\Figure4.pdf", as(pdf) replace
